<template>
  <div class="login-container">
    <div class="logo">
      <img src="../assets/logo.png" alt="CHN ENERGY" />
    </div>
    <div class="login-box">
      <h2 class="login-title">登录</h2>
      <el-form ref="formRef" :model="ruleForm" class="login-form" :rules="rules">
        <el-form-item prop="username">
          <div class="form-group">
            <label>用户名/邮箱</label>
            <el-input
              type="text"
              v-model="ruleForm.username"
              placeholder="请输入用户名"
              class="form-input"
            />
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <div class="form-group">
            <label>密码</label>
            <el-input
              type="password"
              v-model="ruleForm.password"
              placeholder="请输入密码"
              class="form-input"
              show-password
              @keyup.enter="login(formRef)"
            />
          </div>
        </el-form-item>
        <div class="login-button" @click="login(formRef)">登录</div>
      </el-form>
      <div class="footer-text">由国家能源集团研发</div>
    </div>
  </div>
</template>

<script setup >
import { ref, reactive, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
import { Login } from "../api/login.js";
import { useRouter } from "vue-router";
const router = useRouter();
const ruleForm = ref({
  username: "",
  password: "",
});
const formRef = ref(); 
const rules = reactive({
  username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }],
});

const login = async (formEl) => { 
  // 处理登录逻辑

  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const data = {
        username: ruleForm.value.username,
        password: ruleForm.value.password,
      };
      const res = await Login(data);
      if (res.code == 0) {
        localStorage.setItem('username',ruleForm.value.username)
        localStorage.setItem("token", res.data.token);
        localStorage.setItem("isAdmin", res.data.isAdmin);
        ElMessage.success("登陆成功，正在跳转，请稍后。。。");
        setTimeout(() => {
          router.push({
            name: "home",
          });
        }, 1000);
      } else {
        ElMessage.error(res.msg);
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../assets/login-bg.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}

.login-box {
  background: rgba(255, 255, 255, 0.3);
  padding: 56px 94px;
  border-radius: 24px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 690px;
  text-align: center;
}

.logo {
  margin-bottom: 20px;
  position: fixed;
  top: 100px;
  left: 140px;
}

.logo img {
  height: 40px;
}

.login-title {
  font-family: Alibaba PuHuiTi 2, Alibaba PuHuiTi 20;
  font-weight: 700;
  font-size: 38px;
  color: #4478b8;
  line-height: 53px;
  font-style: normal;
  text-transform: none;
  margin-bottom: 24px;
}

.form-group {
  margin-top: 20px;
  text-align: left;
  width: 100%;
}

.form-group label {
  display: block;
  font-family: Alibaba PuHuiTi 2, Alibaba PuHuiTi 20;
  font-weight: 400;
  font-size: 18px;
  color: #5b93d9;
  line-height: 25px;
  text-align: left;
  font-style: normal;
  text-transform: none;
  margin-bottom: 4px;
}

.form-group .el-input {
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  border-radius: 30px;
}

/* .form-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
} */

:deep().el-input__wrapper {
  border-radius: 30px;
  padding: 20px;
  border: 1px solid #a1a8e2;
}

.form-input:focus {
  border-color: #4a6ee0;
  outline: none;
}

.login-button {
  width: 100%;
  height: 60px;
  line-height: 60px;
  background: #5854ce;
  color: white;
  border: none;
  border-radius: 30px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 90px;
}

.login-button:hover {
  background: #3857b3;
}

.footer-text {
  margin-top: 116px;
  font-weight: 400;
  font-size: 14px;
  color: #5b93d9;
}
</style>
